package com.alibaba.dingtalk.openapi.newmessage.entity.template;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Map;
import java.util.Objects;

/**
 * 模板消息
 * @author 魏荣杰
 *
 */
@Setter
@Getter
@ToString
public class TemplateMessage {

    /**
     * 模板id
     */
    private String templateId;

    /**
     * 消息内容
     */
    private FormMessage formMessage;

    public TemplateMessage() {
    }

    public TemplateMessage(String templateId, FormMessage formMessage) {
        this.templateId = templateId;
        this.formMessage = formMessage;
    }


    @Setter
    @Getter
    @ToString
    public static class FormMessage {
        /**
         * 消息框头部内容
         */
        private String head;

        /**
         * 消息标题
         */
        private String title;

        /**
         * 消息内容
         */
        private String content;

        /**
         * 表单内容
         */
        private JSONObject form;

        /**
         * 移动端链接
         */
        private String messageUrl;

        /**
         * pc端链接
         */
        private String pcMessageUrl;

        public JSONObject toJsonObject() {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("head", this.head);
            jsonObject.put("title", this.title);
            jsonObject.put("content", this.content);
            jsonObject.put("messageUrl", this.messageUrl);
            jsonObject.put("pcMessageUrl", this.pcMessageUrl);
            if (!Objects.isNull(this.form)) {
                int i = 1;
                for (Map.Entry<String, Object> obj : this.form.entrySet()) {
                    String key = "key" + i;
                    String value = "value" + i;
                    jsonObject.put(key, obj.getKey());
                    jsonObject.put(value, obj.getValue());
                    i++;
                }
            }
            return jsonObject;
        }

        public FormMessage() {
        }

        public FormMessage(String head, String title, String content, JSONObject form, String messageUrl, String pcMessageUrl) {
            this.head = head;
            this.title = title;
            this.content = content;
            this.form = form;
            this.messageUrl = messageUrl;
            this.pcMessageUrl = pcMessageUrl;
        }
    }


}
